﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks;

namespace MotionControl.Modules.Restful
{
    public class RestService
    {
        //public static RestService GetInstance { get => _instance.Value; }

        //private static readonly Lazy<RestService> _instance = new Lazy<RestService>(() => new RestService(), true);

        private readonly MCMsgServices _mcMsgServices;
        private WebServiceHost _serviceHost;
        private const string defaultURI = "http://127.0.0.1:8888/";
        public RestService()
        {
            _mcMsgServices = new MCMsgServices();
        }
        public void Start(string uriStr = defaultURI)
        {
             _serviceHost?.Close();
            _serviceHost = new WebServiceHost(_mcMsgServices, new Uri(uriStr ?? defaultURI));
            _serviceHost.Open();
        }
        public void RegPLCAction(Action<string> plcAct)
        {
            _mcMsgServices.plcAction = plcAct;
        }
        public void RegLightAction(Action<string> lightAct)
        {
            _mcMsgServices.lightAction = lightAct;
        }
        public void Stop()
        {
            _serviceHost?.Close();
        }
    }
}